#include "stdafx.h"
#include "WMPHotkeys.h"

LRESULT CALLBACK keyHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	if(nCode == HC_ACTION)
	{
		CComPtr<IWMPControls> controls;
		WMPPlayState state;
		CComPtr<IWMPSettings> settings;
		
		long volume = 0;
		static double position = -1;
		//static bool pauseIt = false;

		g_core->get_controls(&controls);
		g_core->get_playState(&state);
		g_core->get_settings(&settings);

		if(lParam & (1 << 31))//transition state 0 means key is being pressed, 1 means key is being released
		{
			switch(wParam)
			{
			case VK_SPACE:
				if (state == wmppsPlaying)
				{
					controls->pause();
				}
				else if ((state == wmppsPaused) || (state == wmppsStopped) || (state == wmppsReady) || (state == wmppsUndefined))
				{
					controls->play();
				}
				break;
			case VK_S:
			case VK_s:
				controls->stop();
				break;
			case VK_N:
			case VK_n:
				controls->next();
				break;
			case VK_P:
			case VK_p:
				controls->previous();
				break;
			}
		}

		if(GetAsyncKeyState(VK_SHIFT) || GetAsyncKeyState(VK_CONTROL))//if shift or control key was pressed
		{
			switch(wParam)
			{
			case VK_UP:
				long volume;
				settings->get_volume(&volume);
				if (volume >= 95)
				{
					settings->put_volume(100);
				}
				else
				{
					if(GetAsyncKeyState(VK_SHIFT))
						settings->put_volume(volume + 2);
					else
						settings->put_volume(volume + 5);
				}
			break;
			case VK_DOWN:
				settings->get_volume(&volume);
				if (volume <= 5)
				{
					settings->put_volume(0);
				}
				else
				{
					if(GetAsyncKeyState(VK_SHIFT))
						settings->put_volume(volume - 2);
					else
						settings->put_volume(volume - 5);
				}
			break;
			case VK_LEFT:
				if(lParam & (1 << 31)) //if key is being released then update the player position
				{
					if(position != -1)
					{
						controls->put_currentPosition(position);
						position = -1; //reset position value
					}
				}
				else //key is being pressed
				{
					if(lParam & (1 << 30 )) //key has been held pressed
					{
						if(GetAsyncKeyState(VK_SHIFT))
							position -= 5;
						else
							position -= 30;
					}
					else //key has been just pressed
					{
						controls->get_currentPosition(&position);
						if(GetAsyncKeyState(VK_SHIFT))
							position -= 5;
						else
							position -= 30;
					}
				}
			break;
			case VK_RIGHT:
				if(lParam & (1 << 31)) //if key is being released then update the player position
				{
					if(position != -1)
					{
						controls->put_currentPosition(position);
						position = -1;
					}
					/*if(pauseIt) //if the playing was paused, resume it
					{
						controls->play();
						pauseIt = false;
					}*/
				}
				else //key is being pressed
				{
					if(lParam & (1 << 30 )) //key is has been held pressed
					{
						if(GetAsyncKeyState(VK_SHIFT))
							position += 5;
						else
							position += 30;
					}
					else //key has been just pressed
					{
						/*if (state == wmppsPlaying) //if player is playing something, just pause it 
						{
							controls->pause();
							pauseIt = true;
						}*/
						controls->get_currentPosition(&position);
						if(GetAsyncKeyState(VK_SHIFT))
							position += 5;
						else
							position += 30;
					}
				}
			break;
			}
		}
	}
	
	return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}